package bookdata.tofile;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.json.JSONObject;

import com.oocl.dao.pojo.Book;
import com.oocl.file2bookdata.FileToBookData;

public class BookDataToFileJson {
	public JSONObject bookData2Json(List<Book> bList){	
		JSONObject jso=new JSONObject();
		for (int i = 0; i < bList.size(); i++) {
			jso.put("book"+i, bList.get(i));
		}
		return jso;
		
	}
	public void jsonProducer(List<Book> bList){
	
		TextMessage msg=null;
		Connection con=null;
		JSONObject jso=bookData2Json(bList);
		ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Destination queue=new ActiveMQQueue("Lionel");
		try {
			con=factory.createConnection();
			con.start();
			Session sen=con.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer mp=sen.createProducer(queue);
			for (int i = 0; i < 10000; i++) {
				msg=(TextMessage)jso.get("book"+i);
				mp.send(msg);
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	public static void main(String[] args) throws Exception{
		List<Book> bList=new ArrayList<Book>();
		FileToBookData ftd=new FileToBookData();
		bList=(List<Book>)ftd.getBookDataFromFile("book.data");
		System.out.println(bList.size());
//		BookDataToFileJson bdtfj=new BookDataToFileJson();
//		bdtfj.bookData2Json(bList);
	}
	

}
